import os


def mvfiles_direct_dir(dirname):
    filenames = os.listdir(dirname)
    for filename in filenames:
        datapath = os.path.join(dirname, filename)
        with open(datapath, "r") as f:
            strs = []
            lines = f.readlines()
            for line in lines:
                if ("trkpt lat" in line):
                    # print(line)
                    str0 = line.split('lat="')[1].split('" lon=')[0]
                    str1 = line.split('lon="')[1].split('">')[0]
                    str = str0 + ' ' + str1 + '\n'
                    strs.append(str)
                    print(str)
            with open(parsed_filepath, "a") as fo:
                fo.writelines(strs)

def mvfiles_1st_indir(first_indir):
    dirnames = os.listdir(first_indir)
    for dirname in dirnames:
        mvfiles_direct_dir(os.path.join(first_indir, dirname))

def mvfiles_2nd_indir(sec_indir):
    first_indirs = os.listdir(sec_indir)
    for first_indir in first_indirs:
        mvfiles_1st_indir(os.path.join(sec_indir, first_indir))

if __name__ == "__main__":
    dataset_dir = "dataset"
    parsed_dir = 'parsed_dir'
    parsed_filename = "parsed_data.txt"
    parsed_filepath = os.path.join(parsed_dir, parsed_filename)

    mvfiles_2nd_indir(dataset_dir)